package com.hulu.yec.teach.testpaper.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



import com.hulu.yec.common.exception.DaoRuntimeException;
import com.hulu.yec.common.util.DBUtil;
import com.hulu.yec.teach.testpaper.entity.TestPaperEntity;

public class PaperAddDao {
	/**
	 * 查询课程名
	 * @return
	 */
	public List<TestPaperEntity> list() {
		List<TestPaperEntity> paramList = new ArrayList<>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			connection = DBUtil.getConnection();
			String sql = "SELECT c.id AS 'courseId' ,c.name AS 'courseName'  FROM sys_course c ,qes_exam_paper p WHERE p.course_id = c.id ";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				TestPaperEntity testPaperEntity = new TestPaperEntity();
				testPaperEntity.setCourseId(resultSet.getInt("courseId"));
				testPaperEntity.setCourseName(resultSet.getString("courseName"));
				paramList.add(testPaperEntity);
			}
		} catch (SQLException e) {
			throw new DaoRuntimeException("sql错误");
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return paramList;
	  }
	
	/**
	 * 新增操作
	 * @param testPaperEntity
	 * @return
	 */
   public int add(TestPaperEntity testPaperEntity){
	int row = 0;
   Connection connection = null;
   PreparedStatement preparedStatement = null;
   try {
	connection= DBUtil.getConnection();
	String sql = "INSERT INTO qes_exam_paper VALUES   (NULL, ?, 'D:', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, 1, NOW(), 1, ?)";
	 preparedStatement = connection.prepareStatement(sql);	
	//设置参数
	 preparedStatement.setString(1, testPaperEntity.getName());
	 preparedStatement.setInt(2, testPaperEntity.getPaperType());
	 preparedStatement.setInt(3, testPaperEntity.getCourseId());
	 preparedStatement.setInt(4, testPaperEntity.getTotalPoint());
	 preparedStatement.setInt(5, testPaperEntity.getPassPoint());
	 preparedStatement.setInt(6, testPaperEntity.getTotalMinutes());
	 preparedStatement.setInt(7, testPaperEntity.getSingleOptionNumber());
	 preparedStatement.setInt(8, testPaperEntity.getSingleOptionEachPoint());
	 preparedStatement.setInt(9, testPaperEntity.getSingleOptionDifficulty());
	 preparedStatement.setInt(10, testPaperEntity.getMultiOptionNumber());
	 preparedStatement.setInt(11, testPaperEntity.getMultiOptionEachPoint());
	 preparedStatement.setInt(12, testPaperEntity.getMultiOptionDifficulty());
	 preparedStatement.setInt(13, testPaperEntity.getJudgeNumber());
	 preparedStatement.setInt(14, testPaperEntity.getJudgeEachPoint());
	 preparedStatement.setInt(15, testPaperEntity.getJudgeDifficulty());
	 preparedStatement.setString(16, testPaperEntity.getMemo());
	 row = preparedStatement.executeUpdate(); 
   } catch (Exception e) {
	  e.printStackTrace();
		}finally{
			DBUtil.close(connection, preparedStatement);
		}
	return row;
   }

}